package com.laikan.reader.bookfriend.activity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.laikan.library.components.activitys.BasicActivity;
import com.laikan.library.managers.ToastMgr;
import com.laikan.library.utils.SPUtil;
import com.laikan.reader.R;
import com.laikan.reader.core.utils.ActivityUitl;
import com.laikan.reader.core.utils.LoadingDialog;
import com.laikan.reader.core.utils.PublicUtil;
import thirdparty.handmark.pulltorefresh.library.PullToRefreshBase;
import thirdparty.handmark.pulltorefresh.library.PullToRefreshWebView;

public class TenpayAndBank extends BasicActivity {

    private static final String APP_CACAHE_DIRNAME = "/webcache";
    private WebView mWebView;
    private PullToRefreshWebView mPullWebView;
    private String url, title;
    private ImageView back_tv;
    private TextView title_tv;
    private LoadingDialog mLoadingDialog;
    private int type;
    private RelativeLayout back_layout;
    Drawable drawable_back_tv;

    private WebViewClient webViewClient = new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            view.loadUrl(url);

            return true;
        }

        public void onPageFinished(WebView view, String url) {

            mPullWebView.onRefreshComplete();
            mWebView.getSettings().setLoadsImagesAutomatically(true);
            mLoadingDialog.dismiss();
            //			if(type == 3){
            //				view.loadUrl("document.querySelectorAll('.js-cls-menu')[0].click()");
            //			}
            //if(mHtmlUrl.equals("http://115.28.143.17:8080/m/accounts/pay/ok_android")){
            if (url.contains("m.laikan.com/accounts/pay/ok_android/")) {
                ToastMgr.showShortToast(R.string.moneypage_pay_success);
                if (SPUtil.getBoolean("signedAction", false)) {
                    LocalBroadcastManager.getInstance(mContext).sendBroadcast(new Intent("action_signed"));
                }
                SPUtil.putBoolean("login_laikan_setting", true);
                mContext.sendBroadcast(new Intent("action_signed"));
            }

        }

        public void onPageStarted(WebView view, String url, android.graphics.Bitmap favicon) {
            mLoadingDialog.show();
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {

            mWebView.loadUrl("file:///android_asset/test.html");
            // super.onReceivedError(view, errorCode, description, failingUrl);
        }
    };

    @Override
    protected void assignViews() {
        setContentView(R.layout.classtwo);
        title_tv = (TextView)findViewById(R.id.tv_title);
        mPullWebView = (PullToRefreshWebView)findViewById(R.id.classifyWebview1);
        back_tv = (ImageView)findViewById(R.id.iv_back);
        back_layout = (RelativeLayout)findViewById(R.id.back_layout);
        mLoadingDialog = new LoadingDialog(mContext);
        drawable_back_tv = new BitmapDrawable(PublicUtil.getAlphaBitmap(PublicUtil.readBitMap(mContext,R.drawable.lk_back_small), Color.parseColor("#66ccdd")));
        back_tv.setImageDrawable(drawable_back_tv);
        back_layout.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                finish();
            }
        });

        url = getIntent().getStringExtra("mHtmlUrl");
        title = getIntent().getStringExtra("title");
        type = getIntent().getIntExtra("type", 0);
        if (type == 2) {

            title_tv.setText(R.string.moneypage_tenpay_pay);
        }else if (type == 3) {
            title_tv.setText(R.string.moneypage_online_bank_pay);
        }
        mWebView = mPullWebView.getRefreshableView();
        WebSettings webSetting = mWebView.getSettings();
        webSetting.setJavaScriptEnabled(true);
        // 判断是否使用硬件加速
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }
        mWebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        // 判断是否有网络，有的话，使用LOAD_DEFAULT，无网络时，使用LOAD_CACHE_ELSE_NETWORK。
        if (ActivityUitl.isNetworkAvailable(mContext)) {
            mWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); // 设置
        }
        // //
        // 缓存模式
        else {
            mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); // 设置 缓存模式
        }
        // 开启 DOM storage API 功能
        mWebView.getSettings().setDomStorageEnabled(true);
        // 开启 database storage API 功能
        mWebView.getSettings().setDatabaseEnabled(true);
        String cacheDirPath = getFilesDir().getAbsolutePath() + APP_CACAHE_DIRNAME;
        // 设置数据库缓存路径
        mWebView.getSettings().setDatabasePath(cacheDirPath);
        // 设置 Application Caches 缓存目录
        mWebView.getSettings().setAppCachePath(cacheDirPath);
        // 开启 Application Caches 功能
        mWebView.getSettings().setAppCacheEnabled(true);
        // view.setBackgroundColor(0);
        // mWebView.setDrawingCacheEnabled(false);

        // 设置图片加载模式
        mWebView.getSettings().setBlockNetworkImage(false);
        // mWebView.getSettings().setBuiltInZoomControls(true);
        mWebView.getSettings().setUseWideViewPort(true);
        // 设置监听
        mWebView.setWebChromeClient(new WebChromeClient());
        mWebView.setWebViewClient(webViewClient);
        mWebView.getSettings().setLoadsImagesAutomatically(false);

        mWebView.loadUrl(url);

        mPullWebView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<WebView>() {

            @Override
            public void onRefresh(PullToRefreshBase<WebView> refreshView) {

                mWebView.loadUrl(url);

            }
        });
    }

    @Override
    protected void initData() {

    }

    public void onBackPressed() {
        finish();
    }

    @Override
    protected void setListener() {

    }

}
